﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace shopform
{
    class Language
    {
     
        /// <summary>
        /// Stores a [key,text] pair representing a row
        /// from the Languages table.
        /// </summary>
        public class LanguageElement
        {
            /// <summary>
            /// Primary Key
            /// </summary>
            private int key;

            /// <summary>
            /// Text to display.
            /// </summary>
            private String text;

            public String Text
            {
                get
                {
                    return text;
                }
            }

            public String Key
            {
                get
                {
                    return key.ToString();
                }
            }


            public LanguageElement(int k, String t)
            {
                this.key = k;
                this.text = t;
            }
        }

        /// <summary>
        /// Current element for iterator.
        /// </summary>
        private int current = -1;

        /// <summary>
        /// Length of Language list.
        /// </summary>
        private int length = 0;
        
        /// <summary>
        /// Array of LanguageElements to store Languages.
        /// </summary>
        public LanguageElement[] Languages;

        public Language()
        {
            try
            {
                //counting records in table
                String query =
                    "SELECT COUNT(*) " +
                    "FROM Languages";
                this.length = (int) myDatabaseCommander.ExecuteScalar(query);
                this.Languages = new LanguageElement[this.length];

                //populating Languages array
                query = "SELECT LanguageID,LanguageName " +
                        "FROM Languages";

                DataTable aDataTable = myDatabaseCommander.ExecuteArray(query);
                int i = 0;
                foreach (DataRow row in aDataTable.Rows)
                {
                    this.Languages[i] = new LanguageElement
                                        (
                                        int.Parse(row["LanguageID"].ToString()),
                                        row["LanguageName"].ToString()
                                        );
                    i++;
                }
                i = 0;
            }
            catch (Exception e)
            {
                throw (new Exception("Language constructor: " + e.Message));
            }
        }

        public bool hasNext()
        {
            if (current < length)
            {
                return true;
            }
            return false;
        }

        public object getCurrent()
        {
            return this.Languages[current];
        }

        public void moveToNext()
        {
            if (current >= length)
            {
                throw new Exception(
                    "Language::moveToNext()::this.current Out of bounds"
                );
            }
            current++;
        }

        public object getFirst()
        {
            current = 0;
            return this.Languages[current];
        }
    }

}